package com.dong.yi18health;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import fragment.fragment1;
import fragment.fragment2;
import fragment.fragment3;

public class GuideActivity extends FragmentActivity implements OnPageChangeListener {

	private ViewPager vp;
	ArrayList<Fragment> datas = new ArrayList<>();
	private ImageView mms[];

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_guide);
		record();
		ininData();
		initView();
		
	}

	private void record() {
		SharedPreferences share = getSharedPreferences("first", Context.MODE_PRIVATE);
		Editor edit = share.edit();
		edit.putBoolean("isfirst", false);
		edit.commit();
	}

	private void ininData() {
		fragment1 f1=new fragment1();
		fragment2 f2=new fragment2();
		fragment3 f3=new fragment3();
		
		datas.add(f1);
		datas.add(f2);
		datas.add(f3);
		
		LinearLayout ll = (LinearLayout) findViewById(R.id.Linearlayout);
		mms = new ImageView[ll.getChildCount()];
		for (int i = 0; i < ll.getChildCount(); i++) {
			ImageView image = (ImageView) ll.getChildAt(i);
			mms[i]=image;
		}
	}
	public void down(View v){
		Button b=(Button) v;
		b.setBackgroundResource(R.drawable.experience_down);
		Intent it=new Intent(GuideActivity.this,MainActivity.class);
		startActivity(it);
		
	}
	private void initView() {
		vp = (ViewPager) findViewById(R.id.viewPager);
		vp.setAdapter(new viewPagerAdapter(getSupportFragmentManager()));
		vp.setOnPageChangeListener(this);

	}

	class viewPagerAdapter extends FragmentPagerAdapter {

		public viewPagerAdapter(FragmentManager fm) {
			super(fm);
			// TODO Auto-generated constructor stub
		}

		@Override
		public Fragment getItem(int arg0) {
			// TODO Auto-generated method stub
			return datas.get(arg0);
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return datas.size();
		}

		

	}

	// ---------------------------------------------------------------------------------
	@Override
	public void onPageScrollStateChanged(int arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		// TODO Auto-generated method stub

	}

	int index = 0;
	@Override
	public void onPageSelected(int arg0) {
		if (arg0 == index) {
			mms[index].setImageResource(R.drawable.page_now);
		} else {
			mms[index].setImageResource(R.drawable.page);
			index = arg0;
			mms[index].setImageResource(R.drawable.page_now);
		}

	}
}
